<?php
class Level extends Admin_Controller
{
	
	function __construct()
	{
		parent::__construct();
		$this->load->model('level_model','level');
	}
	
	function index()
	{
		if(!empty($_GET['search']))
		{
			$this->level->where('levels.name like \'%'.$_GET['search'].'%\'');
		}
		$data['result'] = $this->level->get();
		$data['pagination'] = $this->level->pagination();
		$this->template->build('level_index',$data);
	}
	
	function form($id = NULL)
	{
		$data['rs'] = $this->level->get_row($id);
		$this->template->append_metadata(js_checkbox());
		$this->template->build('level_form',$data);
	}
	
	function save()
	{
		if($_POST)
		{
			if(empty($_POST['id']))
			{
				add_log('เพิ่มสิทธิการใช้งาน "'.$_POST['name'].'"');
			}
			else
			{
				add_log('แก้ไขสิทธิการใช้งาน "'.$_POST['name'].'"');
			}
			if(empty($_POST['manage']))$_POST['manage'] = 0;
			$this->level->save($_POST);
			set_notify('success', lang('save_data_complete'));
		}
		redirect('user/level');
	}
	
	function delete($id)
	{
		$rs = $this->level->get_row($id);
		add_log('ลบสิทธิการใช้งาน "'.$rs['name'].'"');
		$this->level->delete($id);
		set_notify('success', lang('delete_data_complete'));
		redirect($_SERVER['HTTP_REFERER']);
	}
	
}
?>